﻿using DTM.Core.Exceptions;
using DTM.Core.Utils;
using DTM.Domain.Players;
using DTM.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTM.WinApp.Players
{
    /// <summary>
    /// Presenter of players view.
    /// </summary>
    internal class PlayersPresenter : Presenter<IPlayersView>, IPlayersPresenter
    {
        #region Fields

        private readonly IApplicationManager _manager;
        private readonly IPlayerService _service;

        #endregion Fields

        #region Constructors

        public PlayersPresenter(IApplicationManager manager, IPlayerService service)
        {
            Validate.NotNull<ArgumentNullException>(service, "service");
            _service = service;
            Validate.NotNull<ArgumentNullException>(manager, "manager");
            _manager = manager;
        }

        #endregion Constructors

        #region Methods

        protected override void InitalizeView()
        {
            View.AddPlayer += View_AddPlayer;
            View.EditPlayer += View_EditPlayer;

            PolulateDataSource();
        }

        private void PolulateDataSource()
        {
            var players = _service.GetAll();
            View.DataSource = players.ToList();
        }

        private void View_AddPlayer(object sender, EventArgs e)
        {
            var result = _manager.ShowPlayerView();
            if (result)
            {
                PolulateDataSource();
            }
        }

        private void View_EditPlayer(object sender, EventArgs<IPlayer> e)
        {
            var result = _manager.ShowPlayerView(e.Value);
            if (result)
            {
                PolulateDataSource();
            }
        }

        #endregion Methods
    }
}